/**
 * 抽奖方式之九宫格
 *
 * @author jinguangguo
 * @date 2012.9
 */

/**
 * 奖盘 
 */
var diskTips = (function($) {
	
	//页面方面
	(function () {
		
		//复制功能
		$('.copy-btn').die('click').live('click', function () {
			var $copydom = $('<p class="copy-tip"></p>');
			
			if (window.clipboardData) {
				window.clipboardData.setData("Text",$(this).siblings('input').val());
				alert('复制成功');
			} else {
				$(this).siblings('input').select();
				$(this).siblings('.copy-tip').show('slow');
				alert('您当前的浏览器不支持自动复制，请手动复制 ');
			}
		});
		
		//用户信息验证
		window.chkUserContact = function () {
			
			if ($.trim($('#contactDiv').find('.tips-validate').text())) {
				return false;
			}
			
			var v_dom_right = '<i class="v-icon"></i>',
				v_dom_wrong = '<i class="v-icon wrong"></i>',
				$dom = $('#contactDiv'),
				$validate = $('#contactDiv').find('.tips-validate'),
				flag = true;
			
			$dom.find('.v-icon').remove();
			$.trim($dom.find('input[name=realName]').val())
			
			if ($.trim($dom.find('input[name=realName]').val()) === '' 
					|| /^[^u4E00-u9FA5]+$/.test($.trim($dom.find('input[name=realName]').val())) === false) {
				$dom.find('input[name=realName]').after(v_dom_wrong);
				flag = false;
			} else {
				$dom.find('input[name=realName]').after(v_dom_right);
			}
			if ($.trim($dom.find('input[name=phone]').val()) === ''
					|| /^(^(\d{3,4}-)?\d{7,8})$|(1[3,5,8]{1}[0-9]{9})$/.test($.trim($dom.find('input[name=phone]').val())) === false) {
				$dom.find('input[name=phone]').after(v_dom_wrong);
				flag = false;
			} else {
				$dom.find('input[name=phone]').after(v_dom_right);
			}
			if ($.trim($dom.find('input[name=qq]').val()) === ''
					|| /^[0-9]{1,16}$/.test($.trim($dom.find('input[name=qq]').val())) === false) {
				$dom.find('input[name=qq]').after(v_dom_wrong);
				flag = false;
			} else {
				$dom.find('input[name=qq]').after(v_dom_right);
			}
			if ($.trim($dom.find('input[name=addressCode]').val()) === ''
					|| /^\d{6}$/.test($.trim($dom.find('input[name=addressCode]').val())) === false) {
				$dom.find('input[name=addressCode]').after(v_dom_wrong);
				flag = false;
			} else {
				$dom.find('input[name=addressCode]').after(v_dom_right);
			}
			if ($.trim($dom.find('textarea[name=address]').val()) === '' 
					|| $.trim($dom.find('textarea[name=address]').val()).length > 50) {
				$dom.find('textarea[name=address]').after(v_dom_wrong);
				flag = false;
			} else {
				$dom.find('textarea[name=address]').after(v_dom_right);
			}
			if ($.trim($dom.find('select[name=province]').val()) === '' 
					|| $.trim($dom.find('select[name=province]').val()) === '0') {
				$dom.find('.pc').after(v_dom_wrong);
				flag = false;
			} else {
				if ($.trim($dom.find('select[name=city]').val()) === ''
						|| $.trim($dom.find('select[name=city]').val()) === '0') {
					$dom.find('select[name=city]').after(v_dom_wrong);
					flag = false;
				} else {
					$dom.find('.pc').after(v_dom_right);
				}
			}
			
			if (flag && $.trim($validate.text()) === '') {
				return true;
			} else {
				return false;
			}
			
		};
		
	}());
	
	
	/**
	 * 宽度：width
	 * 高度：height
	 * 奖品名称：awardName
	 * G币金额：goldNum
	 * 回调函数：callback
	 */
	function XYTips(option) {
		option.$dom && option.$dom[0] && 
		$.XYTipsWindow({
			___title: "中奖提示框",
			___content: "text:" + option.$dom.html(),
			___showbg: true,
			___drag: "___boxTitle",
			___width: option.width,
			___height: option.height,
			___boxWrapBdColor: "#666",
			___boxBdColor: "#a5a5a5",
			___fnClose: function () {
				//关闭窗口后执行的函数
				if (option.closefunc && typeof option.closefunc === 'function') {
					option.closefunc();
				}
			},
			___fns: function () {
				if (option.callback && typeof option.callback === 'function') {
					option.callback();
				}
				var $bg = $('#XYTipsWindowBg');
				$bg[0] && $bg.css('z-index', $bg.css('z-index') - 1);
			}
		});
	}
	
	var dialog = {
		//实物奖品
		getAward: function (opt) {
			
			var $dom = $('<div class="tips-box" id="tipsBox">\
							   <div class="tips-wrapper tips-w440" id="contactDiv">\
									<div class="tips-item clearfix_2">\
										<div class="tip-info1"><span>恭喜您，获得</span></div>\
									</div>\
									<div class="tips-item mt10">\
										<p>\
											<label><i>*</i>姓名：</label>\
											<input type="text" name="realName" class="input-sohu" placeholder="请输入文字">\
										</p>\
										<p>\
											<label><i>*</i>联系电话：</label>\
											<input type="text" name="phone" class="input-sohu" placeholder="请输入数字">\
										</p>\
										<p>\
											<label><i>*</i>QQ：</label>\
											<input type="text" name="qq" class="input-sohu" placeholder="请输入数字">\
										</p>\
										<p>\
											<label><i>*</i>邮政编码：</label>\
											<input type="text" name="addressCode" class="input-sohu" placeholder="请输入数字">\
										</p>\
										<p>\
											<label><i>*</i>获奖地址：</label>\
											<span class="pc fl">\
												<select name="province" id="ucProvince" class="select-style01"></select>省\
												<select name="city" id="ucCity" class="select-style01"><option>&nbsp;&nbsp;&nbsp;&nbsp;</option></select>市\
											</span>\
										</p>\
										<p>\
											<label><i>*</i>详细地址：</label>\
											<textarea name="address" class="input-sohu" id="ucAddress" placeholder="请输入文字"></textarea>\
											<i>&nbsp;1-50&nbsp;个字符</i>\
										</p>\
									</div>\
									<div class="tips-validate hidden">\
										<span></span>\
									</div>\
									<div class="win-skin-opNav">\
										<a href="javascript:void(0)" class="confirm-btn" id="disk-sure">确定</a>\
										<a href="javascript:void(0)" class="cancel-btn ___closeBox">我不要了</a>\
									</div>\
								</div>\
							</div>');
			$dom.find('.tip-info1').append('<em>' + opt.name + '</em>');
			var realName = '<input type="text" name="realName" value="' + opt.realName + '" id="ucRealName" placeholder="请输入文字" />',
				phone = '<input type="text" name="phone" value="' + opt.phone + '" id="ucPhone" placeholder="请输入数字" />',
				qq = '<input type="text" name="qq" value="' + opt.qq + '" id="ucQq" placeholder="请输入数字" />',
				addressCode = '<input type="text" name="addressCode" value="' + opt.addressCode + '" id="ucAddressCode" placeholder="请输入数字" />';
			$dom.find('input[name=realName]').replaceWith(realName);
			$dom.find('input[name=phone]').replaceWith(phone);
			$dom.find('input[name=qq]').replaceWith(qq);
			$dom.find('input[name=addressCode]').replaceWith(addressCode);
			$dom.find('textarea[name=address]').text(opt.address);
			
			//验证绑定
			(function (){
				
				var validate = function () {
					var v_dom_right = '<i class="v-icon"></i>',
						v_dom_wrong = '<i class="v-icon wrong"></i>';
					
					return function ($elem, reg, text1, text2) {
						var $vdom = $('#contactDiv').find('.tips-validate');
						if ($.trim($elem.val()) === '') {
							doIcon($elem, v_dom_wrong);
							$vdom.text(text1).show();
						} else if (reg && !reg.test($.trim($elem.val()))) {
							doIcon($elem, v_dom_wrong);
							$vdom.text(text2).show();
						} else if ($.trim($elem.val()).length > 50) {
							$vdom.text(text1).show();
							doIcon($elem, v_dom_wrong);
						} else {
							$vdom.text('').hide();
							doIcon($elem, v_dom_right);
						}
						
					}
					
					function doIcon($elem, iDom) {
						$elem.closest('p').children().last().is('i') && $elem.closest('p').children().last().remove();
						iDom && $elem.closest('p').append(iDom);
					}
					
				}();
				
				$('#ucRealName').die('change').live('change', function () {
					validate($(this), /^[^u4E00-u9FA5]+$/, '请填写你的姓名。', '姓名：格式错误，请输入中文字符。');
				});
				$('#ucPhone').die('change').live('change', function () {
					validate($(this), /^(^(\d{3,4}-)?\d{7,8})$|(1[3,5,8]{1}[0-9]{9})$/, '请填写你的联系电话。', '请输入正确的手机号码。');
				});
				$('#ucQq').die('change').live('change', function () {
					validate($(this), /^[0-9]{1,16}$/, '请输入你的QQ号码。', 'QQ号码：格式错误，请输入16位以内的数字。');
				});
				$('#ucAddressCode').die('change').live('change', function () {
					validate($(this), /^\d{6}$/, '请输入你的邮政编码。', '邮政编码：格式错误，请输入6位有效数字。');
				});
				$('#ucProvince').die('change').live('change', function () {
					var $lc = $(this).closest('p').children().last();
					$lc.is('i') && $lc.remove();
					if ($.trim($(this).val()) === '' || $.trim($(this).val()) === '0') {
						$(this).closest('p').append('<i class="v-icon wrong"></i>');
						$('#contactDiv').find('.tips-validate').text('请选择你的省市。');
					} else {
						$('#contactDiv').find('.tips-validate').text('').hide();
						$(this).closest('p').append('<i class="v-icon right"></i>');
					}
				});
				$('#ucCity').die('change').live('change', function () {
					validate($(this), undefined, '请选择你的城市。');
				});
				
				$('#ucAddress').die('keyup').live('keyup', function () {
					var v = $(this).val();
					if ($.trim(v).length > 50) {
						validate($(this), undefined ,'请最多输入50个字符。');
					} else if ($.trim(v).length === 0) {
						validate($(this), undefined ,'请输入你的详细地址。');
					} else {
						validate($(this), undefined ,'');
					}
				});
					
			})();
			
			//省市初始化
			var province = $dom.find("select[name='province']");
			SetProvince(province[0]);
			
			XYTips({
				width: "440px",
				height: "400px",
				$dom: $dom,
				callback: function () {
					$(function () {
						
						//省市列表
						(function (){
							var province = $('.tips-wrapper').find("select[name='province']");
							province.unbind('change').bind('change', function () {
								var $city = $(this).siblings("select[name='city']");
								SetCity(this, $city[0]);
							});
							province.children().each(function () {
								var p = opt.province;
								if ($(this).text() === p) {
									province.val($(this).val());
									province.trigger('change');
									return;
								}
							});
							var city = province.siblings('select[name=city]');
							city.children().each(function () {
								var c = opt.city;
								if ($(this).text() === c) {
									city.val($(this).val());
									return;
								}
							})
						})();
						
					})
				}
			});
			$('#disk-sure').die('click').live('click', function () {
				if (opt.callback && typeof opt.callback === 'function') {
					opt.callback();
				}
			})
		},
		//虚拟奖品--输入卡号
		getGoldWithCardNo: function (opt) {
			var $dom = $('<div class="tips-box" id="tipsBox">\
							   <div class="tips-wrapper">\
									<div class="tips-item clearfix_2">\
										<span class="tip-img"></span>\
										<span class="tip-info">恭喜您，获得</span>\
									</div>\
									<div class="tips-item mt10">\
										<p>\
											<label>卡号：</label>\
											<input type="text" name="cardno" class="input-sohu" />\
											<a href="javascript:void(0)" class="copy-btn">复制</a>\
										</p>\
									</div>\
									<div class="win-skin-opNav">\
										<a href="javascript:void(0)" class="confirm-btn ___closeBox">确定</a>\
									</div>\
								</div>\
							</div>');
			if ($.trim(opt.name).length <= 6) {
				$dom.find('.tip-info').append('<em>' + opt.name + '</em>');
			} else {
				$dom.find('.tips-item').first().append('<span class="award-name">' + opt.name + '</span>');
			}
			var inputCardno = '<input type="text" name="cardno" value="' + opt.cardno + '" readonly="readonly" />';
			$dom.find('input[name=cardno]').replaceWith(inputCardno);
			
			XYTips({
				width: "400px",
				height: "240px",
				$dom: $dom
			});
			
			$('#disk-sure').die('click').live('click', function () {
				if (opt.callback && typeof opt.callback === 'function') {
					opt.callback();
				}
			})
		},
		//虚拟奖品--输入卡号卡密
		getGoldWithCardInfo: function (opt) {
			var $dom = $('<div class="tips-box">\
						   <div class="tips-wrapper tips-pt20">\
								<div class="tips-item clearfix_2">\
									<span class="tip-img"></span>\
									<span class="tip-info">恭喜您，获得：</span>\
								</div>\
								<div class="tips-item mt10">\
									<p>\
										<label>卡号：</label>\
										<input type="text" name="cardno" />\
										<a href="javascript:void(0)" class="copy-btn">复制</a>\
									</p>\
									<p>\
										<label>卡密：</label>\
										<input type="text" name="cardpass" />\
										<a href="javascript:void(0)" class="copy-btn">复制</a>\
									</p>\
								</div>\
								<div class="win-skin-opNav">\
									<a href="javascript:void(0)" class="confirm-btn" id="disk-sure">确定</a>\
								</div>\
							</div>\
						</div>');
			if ($.trim(opt.name).length <= 6) {
				$dom.find('.tip-info').append('<em>' + opt.name + '</em>');
			} else {
				$dom.find('.tips-item').first().append('<span class="award-name">' + opt.name + '</span>');
			}
			
			var inputCardno = '<input type="text" name="cardno" value="' + opt.cardno + '" readonly="readonly" />',
				inputCardpass = '<input type="text" name="cardpass" value="' + opt.cardpass + '" readonly="readonly" />';
			$dom.find('input[name=cardno]').replaceWith(inputCardno);
			$dom.find('input[name=cardpass]').replaceWith(inputCardpass);

			XYTips({
				width: "400px",
				height: "240px",
				$dom: $dom
			});
			
			$('#disk-sure').die('click').live('click', function () {
				if (opt.callback && typeof opt.callback === 'function') {
					opt.callback();
				}
			})
		},
		//得到G币
		getGold: function (opt) {
			var $dom = $('<div class="tips-box">\
						   <div class="tips-wrapper">\
								<div class="tips-item clearfix_2">\
									<span class="tip-img"></span>\
									<span class="tip-info">恭喜您，获得<em></em>币！</span>\
									<a href="http://help.17173.com/news/03032011/182248390.shtml" class="more" target="_blank">如何获得更多G币</a>\
								</div>\
								<div class="win-skin-opNav">\
									<a href="javascript:void(0)" class="confirm-btn" id="disk-sure">确定</a>\
								</div>\
							</div>\
						</div>');
			$dom.find('em').text(opt.gnum + 'G');
			
			XYTips({
				width: "400px",
				height: "200px",
				$dom: $dom
			});
			
			$('#disk-sure').die('click').live('click', function () {
				if (opt.callback && typeof opt.callback === 'function') {
					opt.callback();
				}
			})
		},
		//得到经验值
		getExp: function (opt) {
			var $dom = $('<div class="tips-box">\
						   <div class="tips-wrapper">\
								<div class="tips-item clearfix_2">\
									<span class="tip-img"></span>\
									<span class="tip-info">恭喜您，获得<em></em>经验！</span>\
									<a href="http://my.17173.com/my17173.do?TabID=myExp" class="more" target="_blank">如何获得更多经验</a>\
								</div>\
								<div class="win-skin-opNav">\
									<a href="javascript:void(0)" class="confirm-btn" id="disk-sure">确定</a>\
								</div>\
							</div>\
						</div>');
			$dom.find('em').text(opt.exp);
			XYTips({
				width: "400px",
				height: "200px",
				$dom: $dom
			});
			
			$('#disk-sure').die('click').live('click', function () {
				if (opt.callback && typeof opt.callback === 'function') {
					opt.callback();
				}
			})
		},
		//抽奖失败，谢谢参与
		fail: function (opt) {
			var str = '哎呦，啥也没抽中';
			var $dom = $('<div class="tips-box" id="tipsBox">\
							   <div class="tips-wrapper">\
									<div class="tips-item clearfix_2">\
										<span class="tip-img tip-img2"></span>\
										<span class="tip-info"></span>\
									</div>\
									<div class="win-skin-opNav">\
										<a href="javascript:void(0)" class="confirm-btn" id="disk-sure">确定</a>\
									</div>\
								</div>\
							</div>');
			$dom.find('.tip-info').text(str);
			XYTips({
				width: "400px",
				height: "200px",
				$dom: $dom
			});
			
			$('#disk-sure').die('click').live('click', function () {
				if (opt.callback && typeof opt.callback === 'function') {
					opt.callback();
				}
			})
		},
		//G币不足
		noGold: function (opt) {
			var $dom = $('<div class="tips-box">\
						   <div class="tips-wrapper">\
								<div class="tips-item clearfix_2">\
									<span class="tip-img tip-img1"></span>\
									<span class="tip-info">对不起，您的账号G币不足！</span>\
									<a href="http://help.17173.com/news/03032011/182248390.shtml" class="more" target="_blank">如何获得更多G币</a>\
								</div>\
								<div class="win-skin-opNav">\
									<a href="javascript:void(0)" class="confirm-btn" id="disk-sure">确定</a>\
								</div>\
							</div>\
						</div>');
			XYTips({
				width: "400px",
				height: "200px",
				$dom: $dom
			});
			
			$('#disk-sure').die('click').live('click', function () {
				if (opt.callback && typeof opt.callback === 'function') {
					opt.callback();
				}
			})
		},
		//基本信息不足
		noBaseInfo: function (opt) {
			var $dom = $('<div class="tips-box">\
							   <div class="tips-wrapper">\
									<div class="tips-item clearfix_2">\
										<span class="tip-img tip-img2"></span>\
										<span class="tip-info">对不起，您的基本资料完整度不足！</span>\
									</div>\
									<div class="win-skin-opNav">\
										<a href="javascript:void(0)" class="confirm-btn set-my-btn ___closeBox mr10">立即完善</a>\
										<a href="javascript:void(0)" class="confirm-btn ___closeBox" id="disk-sure">确定</a>\
									</div>\
								</div>\
							</div>');
			XYTips({
				width: "400px",
				height: "200px",
				$dom: $dom
			});
			
			$('#disk-sure').die('click').live('click', function () {
				if (opt.callback && typeof opt.callback === 'function') {
					opt.callback();
				}
			})
			
			$('.set-my-btn').die('click').live('click', function () {
				$.XYTipsWindow({
					___title:"我的个人档案",
					___content:"iframe:http://my.17173.com/win-person-doc.shtml",
					___showbg:true,
					___drag:"___boxTitle",
					___width:"555px",
					___height:"480px",
					___boxWrapBdColor:"#666",
					___boxBdColor:"#a5a5a5",
					___fns: function () {
						$('#XYTipsWindowBg').css('z-index', $('#XYTipsWindowBg').css('z-index') - 1);
					}
				});
			})
		},
		//联系信息不足
		noContactInfo: function (opt) {
			var $dom = $('<div class="tips-box" id="tipsBox">\
							   <div class="tips-wrapper">\
									<div class="tips-item clearfix_2">\
										<span class="tip-img tip-img2"></span>\
										<span class="tip-info">对不起，您的联系资料完整度不足！</span>\
									</div>\
									<div class="win-skin-opNav">\
										<a href="javascript:void(0)" class="confirm-btn ___closeBox mr10 set-my-btn">立即完善</a>\
										<a href="javascript:void(0)" class="confirm-btn ___closeBox" id="disk-sure">确定</a>\
									</div>\
								</div>\
							</div>');
			XYTips({
				width: "400px",
				height: "200px",
				$dom: $dom
			});
			
			$('#disk-sure').die('click').live('click', function () {
				if (opt.callback && typeof opt.callback === 'function') {
					opt.callback();
				}
			})
			
			$('.set-my-btn').die('click').live('click', function () {
				$.XYTipsWindow({
					___title:"我的个人档案",
					___content:"iframe:http://my.17173.com/win-person-doc.shtml",
					___showbg:true,
					___drag:"___boxTitle",
					___width:"555px",
					___height:"480px",
					___boxWrapBdColor:"#666",
					___boxBdColor:"#a5a5a5",
					___fns: function () {
						$('#XYTipsWindowBg').css('z-index', $('#XYTipsWindowBg').css('z-index') - 1);
					}
				});
			})
		},
		//站内等级不足
		noSiteGrade: function (opt) {
			var $dom = $('<div class="tips-box" id="tipsBox">\
							   <div class="tips-wrapper">\
									<div class="tips-item clearfix_2">\
										<span class="tip-img tip-img2"></span>\
										<span class="tip-info">对不起，您的站内等级不够！</span>\
										<a href="http://my.17173.com/my17173.do?TabID=myExp" class="more" target="_blank">如何升级</a>\
									</div>\
									<div class="win-skin-opNav">\
										<a href="javascript:void(0)" class="confirm-btn" id="disk-sure">确定</a>\
									</div>\
								</div>\
							</div>');
			XYTips({
				width: "400px",
				height: "200px",
				$dom: $dom
			});
			
			$('#disk-sure').die('click').live('click', function () {
				if (opt.callback && typeof opt.callback === 'function') {
					opt.callback();
				}
			})
		},
		//抽奖次数用完
		noPlayNum: function (opt) {
			var str = '对不起，您的抽奖次数已经用完！';
			var $dom = $('<div class="tips-box" id="tipsBox">\
							   <div class="tips-wrapper">\
									<div class="tips-item clearfix_2">\
										<span class="tip-img tip-img2"></span>\
										<span class="tip-info"></span>\
									</div>\
									<div class="win-skin-opNav">\
										<a href="javascript:void(0)" class="confirm-btn" id="disk-sure">确定</a>\
									</div>\
								</div>\
							</div>');
			$dom.find('.tip-info').text(str);
			XYTips({
				width: "400px",
				height: "200px",
				$dom: $dom
			});
			
			$('#disk-sure').die('click').live('click', function () {
				if (opt.callback && typeof opt.callback === 'function') {
					opt.callback();
				}
			})
		}
	};
	
	/**
	 * width: 250 | 420 | 580 | 800 | 900
	 * wrapperID: diskWrapper(default)
	 **/
	//九宫格旋转封装
	(function () {
		
		$.fn.extend({
			turn: function(opt) {
				startTurn.call(this, opt);
			}
		});
	
		var DISK = {
	
			ROWS : 3, //表格行数
			COLS : 3, //表格列数
	
			START_INDEX : 2, //开始转动索引
			BUFFER_INDEX : 16, //缓冲索引
			TURN_NUM : 5, //转动圈数
	
			MAX_SPEED : 400, //初始速度
			MIN_SPEED : 40 //峰值速度
	
		};
	
		var startTurn = (function(DISK) {
	
			var index = DISK.START_INDEX, //转动计数索引
				prevIndex = DISK.START_INDEX - 1, //前一位置
				arr = getSideArr(DISK.ROWS, DISK.COLS);//初始化数组
			
			var turnCount,	//转动圈数计数
				quickCount,	//加速格数计数
				slowIndex,	//决定在哪一格开始降速
				timeID,		//时间ID
				table,		//转盘表格
				i = (DISK.MAX_SPEED - DISK.MIN_SPEED) / DISK.BUFFER_INDEX,	//速度增量
				tempSpeed = DISK.MAX_SPEED,	//临时变量
				slowFlag = false,	//降速标志
				endFlag = false,	//结束转动标志
				turnNum = getSlowQuan() + DISK.TURN_NUM;	//前置转动总圈数
			
			return function(opt) {
				
				table = this[0];
				
				//防刷
				$(opt.btn).addClass('disabled').removeClass('link hand');
				
				turnCount = 0;
				quickCount = 0;
				slowIndex = getSlowIndex();	//降速索引
				slowCount = DISK.BUFFER_INDEX;	//缓冲计数
				
				//自定义部分
				//如果出现加载延迟错误
				if (opt.errorFlag) {
					DISK.TURN_NUM = 999999;
				}
	
				clearInterval(timeID);
				timeID = setInterval(control, tempSpeed);
				
				//控制速度
				function control() {
					//变速
					if (endFlag === false) {
						
						//走N格开始加速
						if (quickCount < DISK.BUFFER_INDEX) {
							clearInterval(timeID);
							tempSpeed = tempSpeed - i;
							timeID = setInterval(control, tempSpeed);
							quickCount++;
						}
						//跑N圈后开始减速
						if (turnCount > turnNum) {
							
							if (slowFlag === false && index === slowIndex) {
								//触发结束
								endFlag = true;
								//开始降速
								slowFlag = true;
							}
							
						}
					} 
					if (endFlag === true) {
						
						if (slowFlag === true) {
							
							if (slowCount >= 0) {
								clearInterval(timeID);
								tempSpeed = tempSpeed + i;
								timeID = setInterval(control, tempSpeed);
								slowCount--;
							} else {
								clearInterval(timeID);
								
								$(opt.btn).removeClass('disabled').addClass('link hand');																//改变标志
								endFlag = false;
								slowFlag = false;
								tempSpeed = DISK.MAX_SPEED;
								
								if (opt.callback) {
									opt.callback.call();
									return false;
								}
								return false;
							}
							
						}
						
					}
	
					table.rows[arr[index][0]].cells[arr[index][1]].className = 'turn';
					if (index > 0)
						prevIndex = index - 1;
					else {
						prevIndex = arr.length - 1;
					}
					table.rows[arr[prevIndex][0]].cells[arr[prevIndex][1]].className = '';
					index++;
					
					if (index >= arr.length) {
						index = 0;
						turnCount++;
					} 
					
				}
				
				//取得降速索引
				function getSlowIndex() {
					var yushu = DISK.BUFFER_INDEX % 8;
					var xxx = opt.stopIndex - yushu;
					var res;
					if (xxx < 0) {
						res = opt.stopIndex + (8 - yushu);
					} else {
						res = opt.stopIndex - yushu;
					}
					return res;
				}
	
			};
	
			//定义初始化数组最外圈
			function getSideArr(m, n) {
				//获取数组最外圈
				var resultArr = [];
				var tempX = 0, tempY = 0, direction = "Along", count = 0;
				while (tempX >= 0 && tempX < n && tempY >= 0 && tempY < m && count < m * n) {
					count++;
					resultArr.push([tempY, tempX]);
					if (direction == "Along") {
						if (tempX == n - 1)
							tempY++;
						else
							tempX++;
						if (tempX === n - 1 && tempY === m - 1)
							direction = "Inverse";
					} else {
						if (tempX === 0)
							tempY--;
						else
							tempX--;
						if (tempX === 0 && tempY === 0)
							break;
					}
				}
				return resultArr;
			}
			
			//取得缓冲圈数
			function getSlowQuan() {
				var count = 0;
				var tmp = DISK.BUFFER_INDEX;
				while (tmp > 7) {
					count++;
					tmp = tmp - 8;
				}
				return count;
			}
			
		})(DISK);
		
	})();
	
	return function (opt) {
		
		//先判断中奖类型
		switch (opt.type) {
		//实物奖品
		case 'getAward': 
			dialog['getAward'](opt);
			break;
		//虚拟奖品
		case 'getGold': 
			dialog['getGold'](opt);
			break;
		//虚拟奖品--输入信息--卡号
		case 'getGoldWithCardNo':
			dialog['getGoldWithCardNo'](opt);
			break;
		//虚拟奖品--输入信息--卡号卡密
		case 'getGoldWithCardInfo':
			dialog['getGoldWithCardInfo'](opt);
			break;
		//经验值
		case 'getExp':
			dialog['getExp'](opt);
			break;
		//没有中奖
		case 'fail': 
			dialog['fail'](opt);
			break;
		//G币不足
		case 'noGold': 
			dialog['noGold'](opt);
			break;
		//基本信息不足
		case 'noBaseInfo': 
			dialog['noBaseInfo'](opt);
			break;
		//联系信息不足
		case 'noContactInfo': 
			dialog['noContactInfo'](opt);
			break;
		//站内等级不足
		case 'noSiteGrade': 
			dialog['noSiteGrade'](opt);
			break;
		//抽奖次数用完
		case 'noPlayNum': 
			dialog['noPlayNum'](opt);
			break;
		default: 
			alert('种类不对');
		}
			
	}
	
})(jQuery);

//名单滚动
function scrollNameList() {
	
	if ($('.mod-anl')[0]) {
		
		var $anl = $('.mod-anl'),
			$pWrapper = $('.mod-anl').find('.a-main'),
			$cont = $pWrapper.find('.cont'),
			$p = $cont.find('p'),
			timeID;
		
		//名称字符截取
		$p.each(function () {
			//人名-5汉字符
			$(this).find('a').text(getStrWithLength($.trim($(this).find('a').text()), 10));
			//精品名-9汉字符
			$(this).find('em').text(getStrWithLength($.trim($(this).find('em').text()), 18));
		})
		
		//悬停
		function pHover() {
			$cont.find('p').hover(
				function () {
					clearInterval(timeID);
				},
				function () {
					timeID = window.setInterval(scroll, 3000);
				}
			);
		};
		//名单滚动
		function scroll() {
			$cont.animate({
				'margin-top': '-37px'
			}, 1000, function () {
				$cont.find('p').first().remove().appendTo($cont);
				$cont.css('margin-top', '0px');
			});
		}
		
		//判断尺寸
		if ($anl.siblings().hasClass('disk-450')) {
			
			//450
			if ($p.length > 10) {
				$anl.find('.mod-anl-wrapper').addClass('anl-h418');
				if ($p.length > 11) {
					timeID = window.setInterval(scroll, 3000);
					pHover();
				}
			} else if ($p.length === 0) {
				$cont.empty().append('<div class="no-cont">暂时无中奖名单</div>');
			}
		} else {
			//580
			if ($p.length >=15) {
				$anl.find('.mod-anl-wrapper').addClass('anl-h567');
				if ($p.length > 15) {
					timeID = window.setInterval(scroll, 3000);
					pHover();
				}
			} else if ($p.length === 0) {
				$cont.empty().append('<div class="no-cont">暂时无中奖名单</div>');
			}
		}
		
	}
}

//活动结束后，加上半透明面板
function activeFinished() {
	var $iDisk = $('#disk'),
		$btn = $iDisk.find('#disk-start'),
		$dom = $('<div class="disk-bg"></div>'),
		text = '<em>活动</em><i>已结束</i>';
	$btn.removeClass('link hand').addClass('disabled').unbind('click').html(text);
	$dom.css({
		width: $iDisk.width() + 'px',
		height: $iDisk.height() + 'px'
	})
	$iDisk.append($dom);
}

//提示框
function showTips(str, callback) {
	
	var $bg = $('#XYTipsWindowBg');
	$bg[0] && $bg.css('z-index', $bg.css('z-index') - 1);
	
	var $dom = $('<div class="tips-box" id="tipsBox">\
				   <div class="tips-wrapper">\
						<div class="tips-item clearfix_2">\
							<span class="tip-img"></span>\
							<span class="tip-info"></span>\
						</div>\
						<div class="tips-confirm">\
							<a href="javascript:void(0)" class="tips-btn ___closeBox">确定</a>\
						</div>\
					</div>\
				</div>');
	
	$dom.find('.tip-info').html(str);
	
	$.XYTipsWindow({
		___title:"中奖提示框",
		___content:"text:" + $dom.html(),
		___showbg:true,
		___drag:"___boxTitle",
		___width:"400px",
		___height:"200px",
		___boxWrapBdColor:"#666",
		___boxBdColor:"#a5a5a5",
		___fnClose:function(){
			//关闭窗口后执行的函数
			if (typeof callback === 'function') {
				callback();
			}
		}
	});
	
}

//检查字符数目
function getStrWithLength(str, num) {
    var count = 0,
        resStr = '',
        tempChar;
    
    for (var i = 0; i < str.length; i++) {
        tempChar = str.charAt(i);
        if (/^[\u0391-\uFFE5]+$/.test(tempChar)) {
            count += 2;
            if (count > num) {
                return resStr;
            }
        } else {
            count++;
            if (count > num) {
                return resStr;
            }
        }
        resStr += tempChar;
    }
    return resStr;
}
